package org.example.cglib;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class CglibTest {

    public void print(){
        System.out.println("我是简单的CGLIB代理类的方法");
    }

    public static void main(String[] args) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(CglibTest.class);
        enhancer.setCallback(new MethodInterceptor() {
            @Override
                public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                    System.out.println("开始调用...");
                    System.out.println("代理的方法" + method.getName());
                    System.out.println(methodProxy.getSuperName());
                    Object result = methodProxy.invokeSuper(o,objects);
                    System.out.println("调用完成");
                    return result;
            }
        });
        CglibTest cglib = (CglibTest) enhancer.create();
        cglib.print();
    }
}
